package com.ww.springboot.boot.spring.smartlifecycle;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * todo
 *
 * @author wanwei
 * @since 2021-02-19 14:46
 */
@Component
public class Demo {

    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类）
        applicationContext.register(Demo.class);

        // 启动 Spring 应用上下文
        applicationContext.refresh();
        // 按照类型依赖查找
        SmartLifecycleTestBean smartLifecycleTestBean = applicationContext.getBean(SmartLifecycleTestBean.class);

        smartLifecycleTestBean.setRunning(true);
        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }


    @Bean
    @Primary
    public SmartLifecycleTestBean smartLifecycleTestBean() {
        return new SmartLifecycleTestBean();
    }
}
